<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>添加请假申请-云校通-家长</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
<script src="<%=basePath %>/js/select.js"></script>
<body ontouchstart id="tjjsqj">
	<div class="weui_tab tab-bottom">
		<div class="weui-header bg-blue"> 
	  		<div class="weui-header-left"> <a onclick="back()" href="javascript:" class="icon icon-109 f-white">取消</a> </div>
	    	<h1 class="weui-header-title">请假申请</h1>
	    </div>
		<div class="weui_tab_bd">
	        <div class="weui_cells"style="margin: 0;">
				<div class="weui_cell">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-85 f20 f-blue"></i> 请假人</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input class="weui_input tr" value="${user.parent.studentName }" readonly/>
					</div>
				</div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 开始时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="startDate" class="weui_input weui_input-select" placeholder="请选择" readonly="">
					</div>
				</div>
				<div class="weui_cell weui_cell_select">
					<div class="weui_cell_hd">
						<label class="weui_label"><i class="icon icon-51 f20 f-blue"></i> 结束时间</label>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
						<input id="endDate" class="weui_input weui_input-select" placeholder="请选择" readonly="">
					</div>
				</div>
			</div>
			<div class="weui_cells weui_cells_form">
				<div class="weui_cell weui_cell_select weui_select_after">
	                <div class="weui_cell_hd">
	                    <label class="weui_label"><i class="icon icon-67 f20 f-blue"></i> 请假类型</label>
	                </div>
	                <div class="weui_cell_bd weui_cell_primary">
	                    <select class="weui_select" id="leaveType">
	                        <option value="2">事假</option>
	                        <option value="1">病假</option>
	                    </select>
	                </div>
	           </div>
	            <div class="weui_cell">
	                <div class="weui_cell_bd weui_cell_primary">
	                    <textarea id="leaveReason" class="weui_textarea" placeholder="请详细描述请假原因" rows="3"></textarea>
	                </div>
	            </div>
	        </div>
	        
	        <div class="weui_cells  weui_cells_access">
				<div class="weui_cell">
					<div class="weui_cell_hd" style="margin-right: 5px;">
						<i class="icon icon-54 f18 f-blue"></i>
					</div>
					<div class="weui_cell_bd weui_cell_primary">
	                    <p> 图片</p>
	                </div>
					<div id="filePicker" class="weui_btn weui_btn_mini weui_btn_primary f-white bg-blue">上传</div>
				</div>
			</div>
			<div class="weui_cell moments__post">
				<div class="weui_cell_bd">
					<div class="thumbnails weui_uploader_files">
						<ul id="img1" class="img-click">
						
						</ul>
		           </div>
		       </div>
		   </div>
	       <div class="weui_btn_area" id="leave-addBtn">
		        <a href="javascript:" class="weui_btn bg-blue">提交申请</a>
		    </div>
		    <div style="margin-bottom: 55px;"></div>
		</div>
	</div>
</body>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script type="text/javascript">
	$(function(){
	   var nowDate = getTime(new Date(), 'yyyy-MM-dd');
	   $("#startDate").datetimePicker({title:"选择开始时间",min:nowDate,max:'2050-10-01'});
       $("#endDate").datetimePicker({title:"选择结束时间",min:nowDate,max:'2050-10-01'});
       
       $("#leave-addBtn").click(function(){
       		var startDate = $("#startDate").val();
       		var endDate = $("#endDate").val();
       		var leaveType = $("#leaveType").val();
       		var leaveReason = $("#leaveReason").val();
       		if(startDate.length == 0) {
       			alert("请选择开始时间");
       			return;
       		}
       		if(endDate.length == 0) {
       			alert("请选择结束时间");
       			return;
       		}
       		if(leaveReason.trim().length == 0) {
       			alert("请输入请假原因");
       			return;
       		}
       		var attachIds = new Array();
       		$("#img1 .thumbnail").each(function(i, v) {
       			attachIds.push($(v).attr("data-id"));
       		});
       		$.ajax({
              	url : basePath + "/xs/leave/create",
      	 		type : "post",
      	 		dataType : "json",
      	 		data : {
      	 			startDate: startDate,
      	 			endDate: endDate,
      	 			leaveType: leaveType,
      	 			leaveReason: leaveReason,
      	 			ids: attachIds.toString()
      	 		},
                success: function(data){
                	if(data.code == '000') {
                		$.toast("请假提交成功！");
                		var url = basePath + "/xs/leave?sid=" + queryStringByName("sid");
                		setTimeout("javascript:location.href='"+url+"'", 1000);
                	}else {
                		$.toptips(data.msg);
                	}
                },
                error: function(xhr, type){
                  
                }
             });
       })
        //拍照或从手机相册中选图接口
	    $('#filePicker').on('click', function () {
	        wx.chooseImage({
	            count: 6,
	            needResult: 1,
	            sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
	            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
	            success: function (res) {
	           		var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
	           		wxuploadImage(localIds);
	            },
	            fail: function (res) {
	                alterShowMessage("操作提示", JSON.stringify(res), "1", "确定", "", "", "");
	            }
	
	        });
	    });
		function wxuploadImage(localIds) {  
			var localId = localIds.pop();
	        wx.uploadImage({  
	            localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得  
	            isShowProgressTips: 1, // 默认为1，显示进度提示  
	            success: function (res) {  
	                mediaId = res.serverId; // 返回图片的服务器端ID  
	                wechatMediaDownload(mediaId);
	                if(localIds.length > 0){
	                	wxuploadImage(localIds);
	                }
	            },  
	            fail: function (error) {  
	                alert(Json.stringify(error));  
	            }  
	        });
	   }  
		function wechatMediaDownload(mediaId) {
		   	var params = {};
		   	params.mediaId = mediaId;
	        params.attachmentType = "image";
	        params.relationType = "${relationType}";
	        $.ajax({
		 		url : basePath + "/xs/attchment/upload/wx",
		 		type : "post",
		 		data : params,
		 		dataType : "json",
		 		async : false,
		 		success : function(data) {
		 			if(data.code != '000') {
		 				$.toast(data.msg, "cancel");
		 			}else {
		 				$('#img1').append('<li class="weui_uploader_file thumbnail" data-id="'+data.data.id+'" style="margin-right:0;margin-bottom:16px;position: relative;"><div class="img-del" onclick="imgDel(this)"><span class="icon icon-26"></span></div><img onclick="imgClick(this)" src="'+ imgUrl + data.data.attachmentUrl +'"/></li>'); 
		 			}
		 		}, error:function(){
		 			alert("上传异常");
	      			}
		 	});
	   }
	});
	
	
	function imgClick(thiz) {
		var obj = $(thiz.closest(".img-click")).find("img");
		var src = get_img(obj);
		var currSrc = $(thiz).find('img').attr('src');
		var current;
		for(var i in src) {
			if(src[i] == currSrc) {
				current = i;
				break;
			}
		}
		img_preview(src[current], src);
	}
	function img_preview(current, urls){
        if(!current || urls.length == 0){
	        return ;
        }

        WeixinJSBridge.invoke('imagePreview', { 
            'current': current,
            'urls': urls
        }); 
	}
	function get_img(obj){
		var m = [];
		for(var i=0; i<obj.length; i++){
			m[i] = obj[i].src;
		}
		return m;
	}
</script>
</html>